package com.lanou.controller;

import com.lanou.utils.VerifyCodeUtils;
import com.lanou.utils.code.VerifyCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName VerifyCodeController
 * @Description: TODO
 * @Author 郅占伟
 * @Date 2023年10月30日
 * @Version: 1.0
 */

@Api(value = "登录验证码",tags = "登录验证码")
@RestController
@RequestMapping("/VerifyCode")
public class VerifyCodeController {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @ApiOperation(value = "获取验证码",httpMethod = "GET")
    @RequestMapping("/getCode")
    public String getCode(HttpServletResponse response,String uuid) throws Exception{
        OutputStream outputStream = response.getOutputStream();
        String code = VerifyCodeUtils.generateVerifyCode(4);
        int w = 200,h = 80;

        //String uuid = UUID.randomUUID().toString().replace("-","");
        System.out.println(uuid);

        // 验证码存入Redis
        redisTemplate.opsForValue().set(uuid,code,300);
        redisTemplate.expire("code",300L, TimeUnit.SECONDS);

        // redisTemplate.opsForValue().set("code",code,300L,TimeUnit.SECONDS);

        VerifyCodeUtils.outputImage(w,h,outputStream,code);
        // VerifyCode.getInstance().setCode(code);
        return uuid;
    }
}
